Completed
Push — master ( 51d53a...0b2bf9 )
by greg
02:13
created

post-upload.js ➔ route   A

Complexity

Conditions 3
Paths 41

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 3
c 3
b 1
f 0
nc 41
dl 0
loc 57
rs 9.6818
nop 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A post-upload.js ➔ ... ➔ req.busboy.finish 0 3 1
D post-upload.js ➔ ... ➔ req.busboy.file 0 41 10

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import path from 'path'
2
import fse from 'fs-extra'
3
import mkdirp from 'mkdirp'
4
import limax from 'limax'
5
6
import {
7
  config,
8
  Hooks
9
} from '../../cli'
10
11
var route = function(req, res, next){
12
  Hooks.instance.trigger('beforeRoute', req, res, next)
13
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
15
  var resp = {success: 1}
16
  var filePath
17
  var folderWebPath = '/' + config.upload.image
18
  folderWebPath = Hooks.instance.trigger('beforeSaveImage', folderWebPath, req)
19
  var folderFilePath = path.join(config.root, config.publish.url, folderWebPath)
20
  mkdirp.sync(folderFilePath)
21
  req.pipe(req.busboy)
22
  var finished = false;
23
  req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
24
    var ext = path.extname(filename);
25
    var filenameNoExt = path.basename(filename,ext);
26
    var randID = '-' + (((1+Math.random())*0x100000)|0).toString(16).substring(2)
27
    var slug = limax(filenameNoExt, {separateNumbers: false}) + randID + ext
28
    
29
    filePath = path.join(folderFilePath, slug)
30
    resp['filePath'] = path.join(folderWebPath, slug)
31
32
    var returnErr = function (msg) {
33
      file.resume()
34
      res.set('Content-Type', 'application/json')
35
      res.send(JSON.stringify({error: 1, response: msg}))
36
    }
37
38
    file.on('limit', function() {
39
      returnErr('file is too big')
40
    })
41
42
    if (mimetype !== 'image/jpeg' && mimetype !== 'image/png' && mimetype !== 'image/svg+xml' && mimetype !== 'video/mp4') {
43
      returnErr('unauthorized file')
44
    } else if (ext !== '.jpg' && ext !== '.jpeg' && ext !== '.png' && ext !== '.svg' && ext !== '.mp4') {
45
      returnErr('not a valid asset')
46
    }
47
48
    var fstream = fse.createWriteStream(filePath);
49
50
    fstream.on('finish', function() {
51
      if(/\.(jpg|png|gif|svg)/.test(filePath)){
52
        resp = Hooks.instance.trigger('afterSaveImage', resp, req)
53
      }
54
      // Waiting until the entire request has been fully processed
55
      if (finished) {
56
        res.set('Content-Type', 'application/json')
57
        res.send(JSON.stringify(resp))
58
      }
59
    });
60
61
    file.pipe(fstream);
62
63
  })
64
  req.busboy.on('finish', function() {
65
    finished = true
66
  })
67
}
68
69
export default route